package com.bookpavilion.goods.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.bookpavilion.goods.dao.GoodsDao;
import com.bookpavilion.goods.pojo.Goods;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class GoodsService {

	@Autowired
	private GoodsDao goodsDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Goods> findAll() {
		return goodsDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Goods> findSearch(Map whereMap, int page, int size) {
		Specification<Goods> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return goodsDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Goods> findSearch(Map whereMap) {
		Specification<Goods> specification = createSpecification(whereMap);
		return goodsDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Goods findById(String id) {
		return goodsDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param goods
	 */
	public void add(Goods goods) {
		goods.setId( idWorker.nextId()+"" );
		goods.setComment_nums(0);
		goods.setSale_nums(0);
		goods.setViews(0);
		goods.setStatus(1);
		goods.setCreate_date(new Date());
		goodsDao.save(goods);
	}

	/**
	 * 修改
	 * @param goods
	 */
	public void update(Goods goods) {
		goodsDao.save(goods);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		Goods goods = goodsDao.findById(id).get();
		goods.setStatus(0);
		goodsDao.save(goods);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Goods> createSpecification(Map searchMap) {

		return new Specification<Goods>() {

			@Override
			public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 二手书卖家Id
                if (searchMap.get("seller_id")!=null && !"".equals(searchMap.get("seller_id"))) {
                	predicateList.add(cb.like(root.get("seller_id").as(String.class), "%"+(String)searchMap.get("seller_id")+"%"));
                }
                // 二手书名字
                if (searchMap.get("goodsname")!=null && !"".equals(searchMap.get("goodsname"))) {
                	predicateList.add(cb.like(root.get("goodsname").as(String.class), "%"+(String)searchMap.get("goodsname")+"%"));
                }
                // 图书分类
                if (searchMap.get("category_id")!=null && !"".equals(searchMap.get("category_id"))) {
                	predicateList.add(cb.like(root.get("category_id").as(String.class), "%"+(String)searchMap.get("category_id")+"%"));
                }
                // 图书图片
                if (searchMap.get("small_pic")!=null && !"".equals(searchMap.get("small_pic"))) {
                	predicateList.add(cb.like(root.get("small_pic").as(String.class), "%"+(String)searchMap.get("small_pic")+"%"));
                }
                // 图书简介
                if (searchMap.get("intoduction")!=null && !"".equals(searchMap.get("intoduction"))) {
                	predicateList.add(cb.like(root.get("intoduction").as(String.class), "%"+(String)searchMap.get("intoduction")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
